//  全局静态变量和静态方法
//  Common.m
//  X-Touch2
//
//  Created by shengchao yang on 12-3-7.
//  Copyright (c) 2012年 foxhis. All rights reserved.
//

#import "Common.h"
#import "FileManager.h"
#import "DataBaseAccess.h"
#import "Imports1.h"
#import "DownloadFromInternet.h"
#import "Imports2.h"

@interface Common(Private)
+(void) insertTestData;
@end

@implementation Common
//检查设置信息，并初始化全局参数
+(void) checkSettings{
    NSString *hotelName = [[NSUserDefaults standardUserDefaults] stringForKey:@"hotel_name"];
    if (hotelName == nil){
        [[NSUserDefaults standardUserDefaults] setObject:AppCompany forKey:@"hotel_name"];
        [[NSUserDefaults standardUserDefaults] synchronize];
        hotelName = [[NSUserDefaults standardUserDefaults] stringForKey:@"hotel_name"];
    }
    AppSession.hotelName = hotelName;
    
    NSString *ipadUdid1 = [[NSUserDefaults standardUserDefaults] stringForKey:@"ipad_udid"];
    NSString *newUdid = [MyDeviceUdid replaceFrom:@"-" To:@""];
    if (ipadUdid1 == nil || ![ipadUdid1 isEqualToString:newUdid]){
        [[NSUserDefaults standardUserDefaults] setObject:newUdid forKey:@"ipad_udid"];
        [[NSUserDefaults standardUserDefaults] synchronize];
        ipadUdid1 = [[NSUserDefaults standardUserDefaults] stringForKey:@"ipad_udid"];
    }
	AppSession.ipadUdid = ipadUdid1;
    
    NSString *downloadUrl = [[NSUserDefaults standardUserDefaults] stringForKey:@"download_url"];
	if (downloadUrl == nil){
        [[NSUserDefaults standardUserDefaults] setObject:DefaultUrlPrefix forKey:@"download_url"];
        [[NSUserDefaults standardUserDefaults] synchronize];
        downloadUrl = [[NSUserDefaults standardUserDefaults] stringForKey:@"download_url"];
    }
    AppSession.downloadUrl = downloadUrl;
    
    //从配置中获取支持语言字符串
    NSString *supportLangs = [[NSUserDefaults standardUserDefaults] stringForKey:@"support_langs"];
	if (supportLangs == nil) {
        [[NSUserDefaults standardUserDefaults] setObject:AppSession.supportLangs forKey:@"support_langs"];
        [[NSUserDefaults standardUserDefaults] synchronize];
        supportLangs = [[NSUserDefaults standardUserDefaults] stringForKey:@"support_langs"];
    }
    AppSession.supportLangs = supportLangs;
    
    //分解语言字符串
    AppSession.langsArray = [NSMutableArray arrayWithArray:[AppSession.supportLangs componentsSeparatedByString:@"#"]];
    MyNSLog(@"supportLangs = %@", AppSession.supportLangs);
    
    AppSession.isUpdate = [[NSUserDefaults standardUserDefaults] boolForKey:@"update_switch"];
    AppSession.isOEMDisplay = [[NSUserDefaults standardUserDefaults] boolForKey:@"oem_switch"];
}
//用自定义的测试数据初始化数据库
+(void) initDBWithDefaultData{
    [FileManager fileDelete:DBRealPath];
    [self createTables];
    [self insertTestData];
}
//用自带的数据库初始化数据库
+(void) initDBWithDefaultDB{
    //拷贝数据库文件
    if ([FileManager fileCopy:DBDemoPath TargetName:DBRealPath]) {
        MyNSLog(@"拷贝数据库文件成功!");
    }else{
        MyNSLog(@"拷贝数据库文件失败!!!");
    }
}
+(void) initXRTranslateWithDefault{
    //拷贝翻译对照文件
    if ([FileManager fileCopy:XRTranslateDemoPath TargetName:XRTranslateRealPath]) {
        MyNSLog(@"拷贝翻译对照文件成功!");
    }else{
        MyNSLog(@"拷贝翻译对照文件失败!!!");
    }
}
+(NSString *) translateString:(NSString *)str{
    if (str == nil) {
        return @"";
    }
    if ([AppSession.translate objectForKey:str]) {
        return [self translateImage:str];
    }else {
        return str;
    }
}
+(NSString *) translateImage:(NSString *)img{
   return [[AppSession.translate objectForKey:img] objectForKey:AppSession.language];
}
+(NSString *) jsonfield:(NSString *)des{
    if (des == nil) {
        return @"";
    }
    NSString *searchString      = [NSString stringWithFormat:des] ;
    NSString *regexString       = @"[#'$]" ;  
    NSString *replaceWithString = @"\"" ;  
    NSString *replacedString    = NULL;  
    replacedString = [searchString stringByReplacingOccurrencesOfRegex:regexString withString:replaceWithString];
    
	NSDictionary *tmpDict = [replacedString JSONValue];
	if (tmpDict) {
		if ([tmpDict objectForKey:AppSession.language]) {
            NSString * tempStr = (NSString *)[tmpDict objectForKey:AppSession.language];
			return Trim(tempStr);
		}else {
			return replacedString;
		}
	}else {
		return replacedString;
	}
}
+(NSString *) StringTrim:(NSString *)source{
    if (source == nil) {
        return @"";
    }
    NSString * trimSource = [NSString stringWithFormat:source];
    NSString * des = [trimSource stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    return [NSString stringWithFormat:des];
}
+(id) DictObject:(NSDictionary *) dict item:(NSString *)itemName {
    if ([dict objectForKey:itemName] == nil) {
        return @"";
    }else {
        return Trim((NSString *)[dict objectForKey:itemName]);
    }
}
+(NSString *) formatNilObject:(NSString *)string{
    if(string == nil){
        return @"";
    }else {
        return Trim(string);
    }
}

//NSDate->NSString
+(NSString *) NSDateToNSString:(NSDate *)date Format:(NSString *) format{    
    NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat: format];
    if (date == nil) {
        date = [NSDate date];
    }
    NSString *dateString = [formatter stringFromDate:date];
    [formatter release];
    return dateString;
}

//NSString->NSDate
+(NSDate *) NSStringToNSDate:(NSString *)string Format:(NSString *) format{    
    NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat: format];
    NSDate *date = [formatter dateFromString :string];
    [formatter release];
    return date;
}

+(NSString *) priceFormatter:(NSString *)priceString units:(NSString *)unit{
    float price = [priceString floatValue];
    
    if (unit == nil) {
        return [NSString stringWithFormat:@"￥%@/%@", @"时价", @"份"];
    }else {
        if (price == 0) {//价格为0 显示时价
            return [NSString stringWithFormat:@"￥%@/%@", @"时价", unit];
        }else {//判断是否要显示小数
            float pricef = floorf(price);//只取整数部分
            if (pricef < price) {//如果有小数，则显示两位小数
                return [NSString stringWithFormat:@"￥%0.2f/%@", price, unit];
            }else {//如果是整数，则不显示小数
                return [NSString stringWithFormat:@"￥%d/%@", (int)price, unit];
            } 
        }
    }
}
+(NSString *) numberFormatter:(NSString *)numberString{
    float number = [numberString floatValue];
    NSInteger row1 = (int)floorf(number);
    
    if (number > row1) {
        return numberString;
    }else {
        return [NSString stringWithFormat:@"%d", row1];
    }
}


//建库、建表
+(void) createTables{
    NSString *tableSql_PluBook=@"create table if not exists PluBook( \
                                pbCode text primary key, \
                                pmCode text, \
                                pbDescripts text, \
                                pbImage text, \
                                pbSeq text, \
                                pbStatus text \
                                )";
    [DataBaseAccess Update:tableSql_PluBook];
    NSString *tableSql_PluMenu=@"create table if not exists PluMenu( \
                                pmCode text primary key, \
                                pmDescripts text, \
                                pmImage text, \
                                pmStatus text, \
                                pmSeq text \
                                )";
    [DataBaseAccess Update:tableSql_PluMenu];
    NSString *tableSql_PluSort=@"create table if not exists PluSort( \
                                pmCode text, \
                                psCode text, \
                                psDescripts text, \
                                psImage text, \
                                psStatus text, \
                                psSortType text, \
                                psSpecialSort text, \
                                psRemarks text, \
                                psSeq text \
                                )";
    [DataBaseAccess Update:tableSql_PluSort];
    //菜品
    NSString *tableSql_PluPlu=@"create table if not exists PluPlu( \
                                pmCode text, \
                                psCode text, \
                                ppCode text, \
                                ppTemp text, \
                                ppStatus text, \
                                ppColor text, \
                                ppPosition text, \
                                ppHelpCode text, \
                                ppDescripts text, \
                                ppImage text, \
                                ppPrices text, \
                                ppRecommended text, \
                                ppSeqNormal text, \
                                ppSeqCommend text, \
                                ppType text \
                                )";
    [DataBaseAccess Update:tableSql_PluPlu];
    //食品
    NSString *tableSql_PluFood=@"create table if not exists PluFood( \
                                ppCode text, \
                                pfCooks text, \
                                pfMeterials text, \
                                pfRemarks text \
                                )";
    [DataBaseAccess Update:tableSql_PluFood];
    //香烟
    NSString *tableSql_PluCigarete=@"create table if not exists PluCigarete( \
                                ppCode text, \
                                pcNicotine text, \
                                pcPackages text, \
                                pcTar text, \
                                pcCO text, \
                                pcRemarks text \
                                )";
    [DataBaseAccess Update:tableSql_PluCigarete];
    //酒水
    NSString *tableSql_PluDrink=@"create table if not exists PluDrink( \
                                ppCode text, \
                                pdCapacity text, \
                                pdPlaces text, \
                                pdPrecision text, \
                                pdTempereature text, \
                                pdYear text, \
                                pdRemarks text \
                                )";
    [DataBaseAccess Update:tableSql_PluDrink];
    //套餐
    NSString *tableSql_PluPkg=@"create table if not exists PluPkg( \
                                ppCode text, \
                                pkgCode text, \
                                pkgDescripts text, \
                                pkgTemp text, \
                                pkgPrice text, \
                                pkgUnit text \
                                )";
    [DataBaseAccess Update:tableSql_PluPkg];
    
    NSString *tableSql_PluSpecialSort=@"create table if not exists PluSpecialSort( \
                                pssCode text, \
                                pssDescripts text \
                                )";
    [DataBaseAccess Update:tableSql_PluSpecialSort];
    NSString *tableSql_PluSpecialCode=@"create table if not exists PluSpecialCode( \
                                pscCode text, \
                                pssCode text, \
                                pscDescripts text \
                                )";
    [DataBaseAccess Update:tableSql_PluSpecialCode];
    NSString *tableSql_PluTable=@"create table if not exists PluTable( \
                                ptTableNo text, \
                                ptPcCode text, \
                                ptDescripts text \
                                )";
    [DataBaseAccess Update:tableSql_PluTable];
    NSString *tableSql_PluEmpno=@"create table if not exists PluEmpno( \
                                peEmpno text, \
                                peEmpname text \
                                )";
    [DataBaseAccess Update:tableSql_PluEmpno];
        
    
    //本地餐单
    NSString *tableSql_MyOrderMenus=@"create table if not exists MyOrderMenus( \
                                momLocalMenu text primary key, \
                                pbCode text, \
                                ptTableNo text, \
                                ptDescripts text, \
                                persons text, \
                                peEmpno text, \
                                momDescript text, \
                                momTime text, \
                                momType text, \
                                momSendErr text \
                                )";
    [DataBaseAccess Update:tableSql_MyOrderMenus];
    NSString *tableSql_MyOrderDishes=@"create table if not exists MyOrderDishes( \
                                momLocalMenu text, \
                                pmCode text, \
                                psCode text, \
                                ppCode text, \
                                ppType text, \
                                modDescripts text, \
                                modNumber text, \
                                modPrice text, \
                                modUnits text, \
                                modCook text, \
                                modPriceUpdate text, \
                                modTime text \
                                )";
    [DataBaseAccess Update:tableSql_MyOrderDishes];
    
    //我的套餐明细
    NSString *tableSql_MyOrderPluPkgs=@"create table if not exists MyOrderPluPkgs( \
                                momLocalMenu text, \
                                ppCode text, \
                                pkgCode text, \
                                pkgDescripts text, \
                                pkgTemp text, \
                                pkgNumber text, \
                                pkgPrice text, \
                                pkgCook text, \
                                pkgUnit text \
                                )";
    [DataBaseAccess Update:tableSql_MyOrderPluPkgs];
    
    //远程餐单
    NSString *tableSql_RemoteMenus=@"create table if not exists RemoteMenus( \
                                rmMenu text, \
                                rmTableNo text, \
                                rmTableDescripts text, \
                                rmDate text, \
                                rmPersons text, \
                                rmEmpno text, \
                                rmMode text, \
                                rmPayCode text, \
                                rmSrv text, \
                                rmDsc text, \
                                rmAmount text \
                                )";
    [DataBaseAccess Update:tableSql_RemoteMenus];
    NSString *tableSql_RemoteDishes=@"create table if not exists RemoteDishes( \
                                rmMenu text, \
                                rdInumber text, \
                                rdSort text, \
                                rdCode text, \
                                rdPrintid text, \
                                rdDescripts text, \
                                rdUnits text, \
                                rdPrice text, \
                                rdNumber text, \
                                rdSrv text, \
                                rdDsc text, \
                                rdAmount text, \
                                rdEmpno text, \
                                rdCook text, \
                                rdDate text \
                                )";
    [DataBaseAccess Update:tableSql_RemoteDishes];
    
    //酒店简介+关于图片管理表
    NSString *tableSql_OtherImage=@"create table if not exists OtherImage( \
                                imageId integer primary key autoincrement, \
                                imageType text, \
                                imageName text \
                                )";
    [DataBaseAccess Update:tableSql_OtherImage];    
    MyNSLog(@"建库、建表成功");
}
//插入测试数据
+(void) insertTestData{
    for (int i=0; i<2; i++) {
        [DataBaseAccess Update:[NSString stringWithFormat:@"insert into PluPlu(ppCode, psCode) values('00%d','asdf')",i]];
    }
    for (int i=0; i<2; i++) {
        [DataBaseAccess Update:[NSString stringWithFormat:@"insert into PluBook(pbCode, pbImage) values('00%i','asdf')",i]];
    }
}

+(BOOL) checkPointInRange:(CGPoint) point Range:(CGRect) rect{
    if (point.x<rect.origin.x||point.x>rect.origin.x+rect.size.width||
        point.y<rect.origin.y||point.y>rect.origin.y+rect.size.height) {
        return NO;
    }else{
        return YES;
    }
}


#pragma mark - 数据下载
+(NSMutableArray *) downLoadPluBook{
    NSString *url = [NSString stringWithFormat:@"%@/%@",AppSession.downloadUrl,DownloadUrlPluBook];
	NSArray *array = [DownloadFromInternet downloadJSONData:url Error:nil];//获得Json字符串数组
    NSMutableArray *arrayObj = [[[NSMutableArray alloc] initWithCapacity:[array count]] autorelease];
    for (NSDictionary *item in array) {
        PluBook *temp = [PluBook new];
        [temp setPbCode:DictObject(item, @"pbCode")];
        [temp setPmCode:DictObject(item, @"pmCode")];
        [temp setPbDescripts:DictObject(item, @"pbDescripts")];
        [temp setPbImage:DictObject(item, @"pbImage")];
        [temp setPbSeq:DictObject(item, @"pbSeq")];
        [temp setPbStatus:DictObject(item, @"pbStatus")];
        [arrayObj addObject:temp];
        
        NSString *updateSql = [NSString stringWithFormat:@"insert into PluBook(pbCode,pmCode,pbDescripts,pbImage,pbSeq,pbStatus) values('"];
        updateSql = [updateSql stringByAppendingFormat:@"%@','",temp.pbCode];
        updateSql = [updateSql stringByAppendingFormat:@"%@','",temp.pmCode];
        updateSql = [updateSql stringByAppendingFormat:@"%@','",temp.pbDescripts];
        updateSql = [updateSql stringByAppendingFormat:@"%@','",temp.pbImage];
        updateSql = [updateSql stringByAppendingFormat:@"%@','",temp.pbSeq];
        updateSql = [updateSql stringByAppendingFormat:@"%@')",temp.pbStatus];
        [DataBaseAccess Update:updateSql];
        [temp release];
    }
    
    return arrayObj;
}
+(NSMutableArray *) downLoadPluMenu:(NSString *)pbCode{
    NSString *url = [NSString stringWithFormat:@"%@/%@/%@",AppSession.downloadUrl,DownloadUrlPluMenu,pbCode];
    NSArray *array = [DownloadFromInternet downloadJSONData:url Error:nil];//获得Json字符串数组
    NSMutableArray *arrayObj = [[[NSMutableArray alloc] initWithCapacity:[array count]] autorelease];
    for (NSDictionary *item in array) {
        PluMenu *temp = [PluMenu new];
        [temp setPmCode:DictObject(item, @"pmCode")];
        [temp setPmDescripts:DictObject(item, @"pmDescripts")];
        [temp setPmImage:DictObject(item, @"pmImage")];
        [temp setPmSeq:DictObject(item, @"pmSeq")];
        [temp setPmStatus:DictObject(item, @"pmStatus")];
        [arrayObj addObject:temp];
        
        NSString *updateSql = [NSString stringWithFormat:@"insert into PluMenu(pmCode,pmDescripts,pmImage,pmSeq,pmStatus) values('"];
        updateSql = [updateSql stringByAppendingFormat:@"%@','",temp.pmCode];
        updateSql = [updateSql stringByAppendingFormat:@"%@','",temp.pmDescripts];
        updateSql = [updateSql stringByAppendingFormat:@"%@','",temp.pmImage];
        updateSql = [updateSql stringByAppendingFormat:@"%@','",temp.pmSeq];
        updateSql = [updateSql stringByAppendingFormat:@"%@')",temp.pmStatus];
        [DataBaseAccess Update:updateSql];
        [temp release];
    }
    
    return arrayObj;
}
+(NSMutableArray *) downLoadPluSort:(NSString *)pmCode{
    NSString *url = [NSString stringWithFormat:@"%@/%@/%@",AppSession.downloadUrl,DownloadUrlPluSort,pmCode];
    NSArray *array = [DownloadFromInternet downloadJSONData:url Error:nil];//获得Json字符串数组
    NSMutableArray *arrayObj = [[[NSMutableArray alloc] initWithCapacity:[array count]] autorelease];
    for (NSDictionary *item in array) {
        PluSort *temp = [PluSort new];
        temp.psCode = DictObject(item, @"psCode");
        temp.pmCode = DictObject(item, @"pmCode");
        temp.psDescripts = DictObject(item, @"psDescripts");
        temp.psImage = DictObject(item, @"psImage");
        temp.psStatus = DictObject(item, @"psStatus");
        temp.psSortType = DictObject(item, @"psSortType");
        temp.psSpecialSort = DictObject(item, @"psSpecialSort");
        temp.psRemarks = DictObject(item, @"psRemarks");
        temp.psSeq = DictObject(item, @"psSeq");        
        [arrayObj addObject:temp];
        
        NSString *updateSql = [NSString stringWithFormat:@"insert into PluSort(psCode,pmCode,psDescripts,psImage,psStatus,psSortType,psSpecialSort,psRemarks,psSeq) values('"];
        updateSql = [updateSql stringByAppendingFormat:@"%@','",temp.psCode];
        updateSql = [updateSql stringByAppendingFormat:@"%@','",temp.pmCode];
        updateSql = [updateSql stringByAppendingFormat:@"%@','",temp.psDescripts];
        updateSql = [updateSql stringByAppendingFormat:@"%@','",temp.psImage];
        updateSql = [updateSql stringByAppendingFormat:@"%@','",temp.psStatus];
        updateSql = [updateSql stringByAppendingFormat:@"%@','",temp.psSortType];
        updateSql = [updateSql stringByAppendingFormat:@"%@','",temp.psSpecialSort];
        updateSql = [updateSql stringByAppendingFormat:@"%@','",temp.psRemarks];
        updateSql = [updateSql stringByAppendingFormat:@"%@')",temp.psSeq];
        [DataBaseAccess Update:updateSql];
        [temp release];
    }
    
    return arrayObj;
}
+(NSMutableArray *) downLoadPluPlu:(NSString *)psCode pluMenu:(NSString *)pmCode{
    NSString *url = [NSString stringWithFormat:@"%@/%@/%@",AppSession.downloadUrl,DownloadUrlPluPlu,psCode];
    NSArray *array = [DownloadFromInternet downloadJSONData:url Error:nil];//获得Json字符串数组
    NSMutableArray *arrayObj = [[[NSMutableArray alloc] initWithCapacity:[array count]] autorelease];
    for (NSDictionary *item in array) {
        PluPlu *temp = [PluPlu new];
        temp.pmCode = pmCode;
        temp.psCode = psCode;
        temp.ppCode = DictObject(item, @"ppCode");
        temp.ppTemp = DictObject(item, @"ppTemp");
        temp.ppStatus = DictObject(item, @"ppStatus");
        
        temp.ppColor = DictObject(item, @"ppColor");
        temp.ppPosition = DictObject(item, @"ppPosition");
        temp.ppHelpCode = DictObject(item, @"ppHelpCode");
        temp.ppDescripts = DictObject(item, @"ppDescripts");
        temp.ppImage = DictObject(item, @"ppImage");
        temp.ppRecommended = DictObject(item, @"ppRecommended");
        temp.ppSeqNormal = DictObject(item, @"ppSeqNormal");
        temp.ppSeqCommend = DictObject(item, @"ppSeqCommend");
        temp.ppType = DictObject(item, @"ppType");
        [arrayObj addObject:temp];
        
        temp.ppTemp = [temp.ppTemp isEqualToString:@"F"] ? @"0":@"1";
        
        NSString *updateSql = [NSString stringWithFormat:@"insert into PluPlu(pmCode,psCode,ppCode,ppTemp,ppStatus,ppColor,ppPosition,ppHelpCode,ppDescripts,ppImage,ppPrices,ppRecommended,ppSeqNormal,ppSeqCommend,ppType) values('"];
        updateSql = [updateSql stringByAppendingFormat:@"%@','",pmCode];
        updateSql = [updateSql stringByAppendingFormat:@"%@','",temp.psCode];
        updateSql = [updateSql stringByAppendingFormat:@"%@','",temp.ppCode];
        updateSql = [updateSql stringByAppendingFormat:@"%@','",temp.ppTemp];
        updateSql = [updateSql stringByAppendingFormat:@"%@','",temp.ppStatus];
        
        updateSql = [updateSql stringByAppendingFormat:@"%@','",temp.ppColor];
        updateSql = [updateSql stringByAppendingFormat:@"%@','",temp.ppPosition];
        updateSql = [updateSql stringByAppendingFormat:@"%@','",temp.ppHelpCode];
        updateSql = [updateSql stringByAppendingFormat:@"%@','",temp.ppDescripts];
        updateSql = [updateSql stringByAppendingFormat:@"%@','",temp.ppImage];
        updateSql = [updateSql stringByAppendingFormat:@"%@','",DictObject(item, @"ppPrices")];
        updateSql = [updateSql stringByAppendingFormat:@"%@','",temp.ppRecommended];
        updateSql = [updateSql stringByAppendingFormat:@"%@','",temp.ppSeqNormal];
        updateSql = [updateSql stringByAppendingFormat:@"%@','",temp.ppSeqCommend];
        updateSql = [updateSql stringByAppendingFormat:@"%@')",temp.ppType];
        [DataBaseAccess Update:updateSql];
        [temp release];
    }
    
    return arrayObj;
}
+(void) downLoadPluFood:(NSString *)ppCode{
    NSString *url = [NSString stringWithFormat:@"%@/%@/%@",AppSession.downloadUrl,DownloadUrlPluFood,ppCode];
    NSArray *array = [DownloadFromInternet downloadJSONData:url Error:nil];//获得Json字符串数组
    for (NSDictionary *item in array) {        
        NSString *updateSql = [NSString stringWithFormat:@"insert into PluFood(ppCode,pfCooks,pfMeterials,pfRemarks) values('"];
        updateSql = [updateSql stringByAppendingFormat:@"%@','",ppCode];
        updateSql = [updateSql stringByAppendingFormat:@"%@','",DictObject(item, @"pfCooks")];
        updateSql = [updateSql stringByAppendingFormat:@"%@','",DictObject(item, @"pfMeterials")];
        updateSql = [updateSql stringByAppendingFormat:@"%@')",DictObject(item, @"pfRemarks")];
        [DataBaseAccess Update:updateSql];
    }
}
+(void) downLoadPluCigarete:(NSString *)ppCode{
    NSString *url = [NSString stringWithFormat:@"%@/%@/%@",AppSession.downloadUrl,DownloadUrlPluCigarete,ppCode];
    NSArray *array = [DownloadFromInternet downloadJSONData:url Error:nil];//获得Json字符串数组
    for (NSDictionary *item in array) {        
        NSString *updateSql = [NSString stringWithFormat:@"insert into PluCigarete(ppCode,pcNicotine,pcPackages,pcTar,pcCO,pcRemarks) values('"];
        updateSql = [updateSql stringByAppendingFormat:@"%@','",ppCode];
        updateSql = [updateSql stringByAppendingFormat:@"%@','",DictObject(item, @"pcNicotine")];
        updateSql = [updateSql stringByAppendingFormat:@"%@','",DictObject(item, @"pcPackages")];
        updateSql = [updateSql stringByAppendingFormat:@"%@','",DictObject(item, @"pcTar")];
        updateSql = [updateSql stringByAppendingFormat:@"%@','",DictObject(item, @"pcCO")];
        updateSql = [updateSql stringByAppendingFormat:@"%@')",DictObject(item, @"pcRemarks")];
        [DataBaseAccess Update:updateSql];
    }
}
+(void) downLoadPluDrink:(NSString *)ppCode{
    NSString *url = [NSString stringWithFormat:@"%@/%@/%@",AppSession.downloadUrl,DownloadUrlPluDrink,ppCode];
    NSArray *array = [DownloadFromInternet downloadJSONData:url Error:nil];//获得Json字符串数组
    for (NSDictionary *item in array) {
        NSString *updateSql = [NSString stringWithFormat:@"insert into PluDrink(ppCode,pdCapacity,pdPlaces,pdPrecision,pdTempereature,pdYear,pdRemarks) values('"];
        updateSql = [updateSql stringByAppendingFormat:@"%@','",ppCode];
        updateSql = [updateSql stringByAppendingFormat:@"%@','",DictObject(item, @"pdCapacity")];
        updateSql = [updateSql stringByAppendingFormat:@"%@','",DictObject(item, @"pdPlaces")];
        updateSql = [updateSql stringByAppendingFormat:@"%@','",DictObject(item, @"pdPrecision")];
        updateSql = [updateSql stringByAppendingFormat:@"%@','",DictObject(item, @"pdTempereature")];
        updateSql = [updateSql stringByAppendingFormat:@"%@','",DictObject(item, @"pdYear")];
        updateSql = [updateSql stringByAppendingFormat:@"%@')",DictObject(item, @"pdRemarks")];
        [DataBaseAccess Update:updateSql];
    }
}
+(void) downLoadPluPkg:(NSString *)ppCode{
    NSString *url = [NSString stringWithFormat:@"%@/%@/%@",AppSession.downloadUrl,DownloadUrlPluPkg,ppCode];
    NSArray *array = [DownloadFromInternet downloadJSONData:url Error:nil];//获得Json字符串数组
    for (NSDictionary *item in array) {
        NSString *updateSql = [NSString stringWithFormat:@"insert into PluPkg(ppCode,pkgCode,pkgDescripts,pkgTemp,pkgPrice,pkgUnit) values('"];
        updateSql = [updateSql stringByAppendingFormat:@"%@','",ppCode];
        updateSql = [updateSql stringByAppendingFormat:@"%@','",DictObject(item, @"pkgCode")];
        updateSql = [updateSql stringByAppendingFormat:@"%@','",DictObject(item, @"pkgDescripts")];
        updateSql = [updateSql stringByAppendingFormat:@"%@','",DictObject(item, @"pkgTemp")];
        updateSql = [updateSql stringByAppendingFormat:@"%@','",DictObject(item, @"pkgPrice")];
        updateSql = [updateSql stringByAppendingFormat:@"%@')",DictObject(item, @"pkgUnit")];
        [DataBaseAccess Update:updateSql];
    }
}
+(NSMutableArray *) downLoadPluSpecialSort{
    NSString *url = [NSString stringWithFormat:@"%@/%@",AppSession.downloadUrl,DownloadUrlPluSpecialSort];
    NSArray *array = [DownloadFromInternet downloadJSONData:url Error:nil];//获得Json字符串数组
    NSMutableArray *arrayObj = [[[NSMutableArray alloc] initWithCapacity:[array count]] autorelease];
    for (NSDictionary *item in array) {
        PluSpecialSort *temp = [PluSpecialSort new];
        temp.pssCode = DictObject(item, @"pssCode");
        temp.pssDescripts = DictObject(item, @"pssDescripts");
        [arrayObj addObject:temp];
        
        NSString *updateSql = [NSString stringWithFormat:@"insert into PluSpecialSort(pssCode,pssDescripts) values('"];
        updateSql = [updateSql stringByAppendingFormat:@"%@','",temp.pssCode];
        updateSql = [updateSql stringByAppendingFormat:@"%@')",temp.pssDescripts];
        [DataBaseAccess Update:updateSql];
        [temp release];
    }
    
    return arrayObj;
}
+(void) downLoadPluSpecialCode:(NSString *)pssCode{
    NSString *url = [NSString stringWithFormat:@"%@/%@/%@",AppSession.downloadUrl,DownloadUrlPluSpecialCode,pssCode];
    NSArray *array = [DownloadFromInternet downloadJSONData:url Error:nil];//获得Json字符串数组
    for (NSDictionary *item in array) {
        NSString *updateSql = [NSString stringWithFormat:@"insert into PluSpecialCode(pscCode,pssCode,pscDescripts) values('"];
        updateSql = [updateSql stringByAppendingFormat:@"%@','",DictObject(item, @"pscCode")];
        updateSql = [updateSql stringByAppendingFormat:@"%@','",DictObject(item, @"pssCode")];
        updateSql = [updateSql stringByAppendingFormat:@"%@')",DictObject(item, @"pscDescripts")];
        [DataBaseAccess Update:updateSql];
    }
}
+(NSInteger) downLoadPluTable{
    NSString *url = [NSString stringWithFormat:@"%@/%@/%@",AppSession.downloadUrl,DownloadUrlPluTable,AppSession.ipadUdid];
    NSArray *array = [DownloadFromInternet downloadJSONData:url Error:nil];//获得Json字符串数组
    for (NSDictionary *item in array) {
        NSString *updateSql = [NSString stringWithFormat:@"insert into PluTable(ptTableNo,ptPcCode,ptDescripts) values('"];
        updateSql = [updateSql stringByAppendingFormat:@"%@','",DictObject(item, @"ptTableNo")];
        updateSql = [updateSql stringByAppendingFormat:@"%@','",DictObject(item, @"ptPcCode")];
        updateSql = [updateSql stringByAppendingFormat:@"%@')",DictObject(item, @"ptDescripts")];
        [DataBaseAccess Update:updateSql];
    }
    return [array count];
}
+(NSInteger) downLoadPluEmpno{
    NSString *url = [NSString stringWithFormat:@"%@/%@",AppSession.downloadUrl,DownloadUrlPluEmpno];
    NSArray *array = [DownloadFromInternet downloadJSONData:url Error:nil];//获得Json字符串数组
    for (NSDictionary *item in array) {
        NSString *updateSql = [NSString stringWithFormat:@"insert into PluEmpno(peEmpno,peEmpname) values('"];
        updateSql = [updateSql stringByAppendingFormat:@"%@','",DictObject(item, @"peEmpno")];
        updateSql = [updateSql stringByAppendingFormat:@"%@')",DictObject(item, @"peEmpname")];
        [DataBaseAccess Update:updateSql];
    }
    return [array count];
}
+(void) downLoadRemoteMenus:(NSString *)searchText{    
    //下载远程主单信息
    NSString *url = [NSString stringWithFormat:@"%@/%@/%@/%@",AppSession.downloadUrl,RemoteMenusUrl,searchText,AppSession.ipadUdid];
    NSArray *array = [DownloadFromInternet downloadJSONData:url Error:nil];//获得Json字符串数组
    for (NSDictionary *item in array) {
        RemoteMenus *temp = [RemoteMenus new];
        temp.rmMenu = DictObject(item, @"rmMenu");
        temp.rmTableNo = DictObject(item, @"rmTableNo");
        temp.rmTableDescripts = DictObject(item, @"rmTableDescripts");
        temp.rmDate = DictObject(item, @"rmDate");
        temp.rmPersons = DictObject(item, @"rmPersons");
        temp.rmEmpno = DictObject(item, @"rmEmpno");
        temp.rmMode = DictObject(item, @"rmMode");
        temp.rmPayCode = DictObject(item, @"rmPayCode");
        temp.rmSrv = DictObject(item, @"rmSrv");
        temp.rmDsc = DictObject(item, @"rmDsc");
        temp.rmAmount = DictObject(item, @"rmAmount");
        
        NSString *updateSql = [NSString stringWithFormat:@"insert into RemoteMenus(rmMenu,rmTableNo,rmTableDescripts,rmDate,rmPersons,rmEmpno,rmMode,rmPayCode,rmSrv,rmDsc,rmAmount) values('"];
        updateSql = [updateSql stringByAppendingFormat:@"%@','",temp.rmMenu];
        updateSql = [updateSql stringByAppendingFormat:@"%@','",temp.rmTableNo];
        updateSql = [updateSql stringByAppendingFormat:@"%@','",temp.rmTableDescripts];
        updateSql = [updateSql stringByAppendingFormat:@"%@','",temp.rmDate];
        updateSql = [updateSql stringByAppendingFormat:@"%@','",temp.rmPersons];
        updateSql = [updateSql stringByAppendingFormat:@"%@','",temp.rmEmpno];
        updateSql = [updateSql stringByAppendingFormat:@"%@','",temp.rmMode];
        updateSql = [updateSql stringByAppendingFormat:@"%@','",temp.rmPayCode];
        updateSql = [updateSql stringByAppendingFormat:@"%@','",temp.rmSrv];
        updateSql = [updateSql stringByAppendingFormat:@"%@','",temp.rmDsc];
        updateSql = [updateSql stringByAppendingFormat:@"%@')",temp.rmAmount];
        [DataBaseAccess Update:updateSql];
        [temp release];
    }
}
+(void) downLoadRemoteDishes:(NSString *)rmMenu{//下载远程主单明细
    NSString *url = [NSString stringWithFormat:@"%@/%@/%@/%@",AppSession.downloadUrl,RemoteDishesUrl,rmMenu,AppSession.ipadUdid];
    NSArray *array = [DownloadFromInternet downloadJSONData:url Error:nil];//获得Json字符串数组
    for (NSDictionary *item in array) {
        RemoteDishes *temp = [RemoteDishes new];
        temp.rmMenu = DictObject(item, @"rmMenu");
        temp.rdInumber = DictObject(item, @"rdInumber");
        temp.rdSort = DictObject(item, @"rdSort");
        temp.rdCode = DictObject(item, @"rdCode");
        temp.rdPrintid = DictObject(item, @"rdPrintid");
        temp.rdDescripts = DictObject(item, @"rdDescripts");
        temp.rdUnits = DictObject(item, @"rdUnits");
        temp.rdPrice = DictObject(item, @"rdPrice");
        temp.rdNumber = DictObject(item, @"rdNumber");
        temp.rdSrv = DictObject(item, @"rdSrv");
        temp.rdDsc = DictObject(item, @"rdDsc");
        temp.rdAmount = DictObject(item, @"rdAmount");
        temp.rdEmpno = DictObject(item, @"rdEmpno");
        temp.rdCook = DictObject(item, @"rdCook");
        temp.rdDate = DictObject(item, @"rdDate");
        
        NSString *updateSql = [NSString stringWithFormat:@"insert into RemoteDishes(rmMenu,rdInumber,rdSort,rdCode,rdPrintid,rdDescripts,rdUnits,rdPrice,rdNumber,rdSrv,rdDsc,rdAmount,rdEmpno,rdCook,rdDate) values('"];
        updateSql = [updateSql stringByAppendingFormat:@"%@','",temp.rmMenu];
        updateSql = [updateSql stringByAppendingFormat:@"%@','",temp.rdInumber];
        updateSql = [updateSql stringByAppendingFormat:@"%@','",temp.rdSort];
        updateSql = [updateSql stringByAppendingFormat:@"%@','",temp.rdCode];
        updateSql = [updateSql stringByAppendingFormat:@"%@','",temp.rdPrintid];
        updateSql = [updateSql stringByAppendingFormat:@"%@','",temp.rdDescripts];
        updateSql = [updateSql stringByAppendingFormat:@"%@','",temp.rdUnits];
        updateSql = [updateSql stringByAppendingFormat:@"%@','",temp.rdPrice];
        updateSql = [updateSql stringByAppendingFormat:@"%@','",temp.rdNumber];
        updateSql = [updateSql stringByAppendingFormat:@"%@','",temp.rdSrv];
        updateSql = [updateSql stringByAppendingFormat:@"%@','",temp.rdDsc];
        updateSql = [updateSql stringByAppendingFormat:@"%@','",temp.rdAmount];
        updateSql = [updateSql stringByAppendingFormat:@"%@','",temp.rdEmpno];
        updateSql = [updateSql stringByAppendingFormat:@"%@','",temp.rdCook];
        updateSql = [updateSql stringByAppendingFormat:@"%@')",temp.rdDate];
        [DataBaseAccess Update:updateSql];
        [temp release];
    }
}

+(NSMutableArray *) downLoadOtherImage:(NSString *)imageType{
    NSString *url = [NSString stringWithFormat:@"%@/update/%@",AppSession.downloadUrl,imageType];
	NSArray *array = [DownloadFromInternet downloadJSONData:url Error:nil];//获得Json字符串数组
    NSMutableArray *arrayObj = [[[NSMutableArray alloc] initWithCapacity:[array count]] autorelease];
    for (NSDictionary *item in array) {
        OtherImage *temp = [OtherImage new];
        temp.imageType = imageType;
        temp.imageName = DictObject(item, @"image");
        [arrayObj addObject:temp];
        
        NSString *updateSql = [NSString stringWithFormat:@"insert into OtherImage(imageType,imageName) values('"];
        updateSql = [updateSql stringByAppendingFormat:@"%@','",temp.imageType];
        updateSql = [updateSql stringByAppendingFormat:@"%@')",temp.imageName];
        [DataBaseAccess Update:updateSql];
        [temp release];
    }
    
    return arrayObj;
}


+(BOOL) downLoadImageFixedURL:(NSString *)fixedURL folderName:(NSString *)folderName imageName:(NSString *)imageName{
    NSString *url = [NSString stringWithFormat:@"%@/%@/%@",AppSession.downloadUrl,fixedURL,imageName];//图片的网络地址
    NSString *folderPath = [DocumentPath stringByAppendingPathComponent:folderName];//图片存放的本地目录
    NSString *filePath = [folderPath stringByAppendingPathComponent:imageName];//下载文件的目标地址
    BOOL isImageExists = YES;
    if ([FileManager folderCreate:folderPath]) {
        if (![FileDefaultManager fileExistsAtPath:filePath]) {
            [DownloadFromInternet downloadFile:url LocalFilePath:filePath];
        }
        
        //判断文件大小
        NSDictionary *fileAttributes1 = [FileDefaultManager attributesOfItemAtPath:filePath error:nil];
        if (fileAttributes1 != nil) {
            NSNumber *size = [fileAttributes1 objectForKey:NSFileSize];
            MyNSLog(@"size=%d", [size intValue]);
            if ([size intValue] < 2000) {
                isImageExists = NO;
                [FileManager fileDelete:filePath];
            }else {
                isImageExists = YES;
            }
        }
    }
    return isImageExists;
}
@end